package com.xauto.pioneer.appuser.activity.mine.user;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.ta.utdid2.android.utils.StringUtils;
import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.BaseActivity;
import com.xauto.pioneer.appuser.http.BaseLHttpHandler;
import com.xauto.pioneer.appuser.http.LHttpLib;
import com.xauto.pioneer.appuser.model.JSONStatus;
import com.xauto.pioneer.appuser.model.WebInfo;
import com.xauto.pioneer.appuser.view.TopBar;

import butterknife.Bind;

/**
 * webView
 * Created by Lemon on 16/5/13.
 */
public class GuideDetailActivity extends BaseActivity {
    @Bind(R.id.topBar)
    TopBar topBar;
    @Bind(R.id.webView)
    WebView webView;
    String guideId = "";
    /**
     * 入口
     * @param mContext
     */
    public static void startAction(Context mContext, String guideId,String title) {
        Intent intent = new Intent(mContext, GuideDetailActivity.class);
        intent.putExtra("guideId", guideId);
        intent.putExtra("title", title);
        mContext.startActivity(intent);
    }

    @Override
    protected int getContentView() {
        return R.layout.ac_webview;
    }

    @Override
    protected void initView() {
        topBar.showView(TopBar.TITLE, TopBar.LEFT_IMAGE_BTN);
        topBar.setText(TopBar.TITLE, StringUtils.isEmpty(getIntent().getStringExtra("title")) ? "" : getIntent().getStringExtra("title"));
        topBar.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
        topBar.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void initData() {
        guideId = getIntent().getStringExtra("guideId");
        WebSettings webSettings = webView.getSettings();
        webSettings.setDefaultTextEncodingName("UTF -8");//设置默认为utf-8
        webSettings.setSavePassword(false);
        webSettings.setSaveFormData(false);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(false);
        webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
       loadData();
    }

    /**
     * 加载网页信息
     */
    private void loadData() {
        new LHttpLib().getGuideDetail(mContext,guideId, new BaseLHttpHandler(mContext, true, true) {
            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                final WebInfo webInfo = new WebInfo().parse(jsonStatus.data);
                if (webInfo == null) return;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        topBar.setText(TopBar.TITLE, webInfo.data_value.title);
                        if ("1".equals(webInfo.data_type)) {
                            webView.loadUrl(webInfo.data_value.content);
                        } else {
                            webView.loadData(webInfo.data_value.content, "text/html; charset=UTF-8", null);//这种写法可以正确解码
                        }
                    }
                });

            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

}
